<template>
  <view style="width: 100%">
    <view class="li-uploader">
      <block v-if="!isSinglePic">
        <view class="li-uploader-body">
          <view class="li-uploader__files" style="flex-direction: row">
            <block v-for="(image, index) in imgFiles" :key="index">
              <view class="li-uploader__file">
                <image
                  class="li-uploader__img"
                  :src="rangeKey == '' ? image : image[rangeKey]"
                  @click="
                    uPreviewImage(rangeKey == '' ? image : image[rangeKey])
                  "
                >
                </image>
                <view class="icon-close">
                  <image
                    @click="deleteImg(index)"
                    mode="widthFix"
                    src="/static/images/icon/close.png"
                    style="width: 20px"
                  ></image
                ></view>
              </view>
            </block>
            <view
              class="li-uploader__input-box"
              v-if="imgFiles.length < maxCount"
            >
              <view class="li-uploader__input" @tap="chooseImage"></view>
            </view>
          </view>
        </view>
        <view class="li-uploader-head" v-if="showBottomText">
          <view class="li-uploader-title">点击可预览选好的图片</view>
          <view class="li-uploader-info"
            >{{ imgFiles.length }}/{{ maxCount }}</view
          >
        </view>
      </block>
      <block v-else>
        <view class="li-uploader__file" v-if="imgUrl.trim().length > 0">
          <image
            class="li-uploader__img"
            :src="imgUrl"
            @click="uPreviewImage(imgUrl)"
          >
          </image>
          <view class="icon-close">
            <image
              @click="deleteImg(666)"
              mode="widthFix"
              src="/static/images/icon/close.png"
              style="width: 20px"
            ></image
          ></view>
        </view>
        <view v-else class="li-uploader__input-box">
          <view class="li-uploader__input" @tap="chooseImage"></view>
        </view>
      </block>
    </view>
  </view>
</template>
<script>
import { utoast, uPreviewImage, uploadImage } from "../../utils/uniUtils";
export default {
  name: "LiUploadImg",
  props: {
    imgFiles: {
      type: Array,
    },
    rangeKey: {
      type: String,
      default: "",
    },
    maxCount: {
      type: Number,
      default: 20,
    },
    showBottomText: {
      type: Boolean,
      default: true,
    },
    isSinglePic: {
      type: Boolean,
      default: false,
    },
    imgUrl: {
      type: String,
      default: "",
    },
  },
  methods: {
    uPreviewImage,
    async chooseImage() {
      if (this.imgFiles.length >= this.maxCount) {
        utoast("图片不能超过" + this.maxCount + "张~");
        return;
      }
      // 上传
      const imgSrc = await uploadImage();
      this.$emit("afterChooseImage", { imgSrc });
    },
    deleteImg(index) {
      this.$emit("deleteImg", { idx: index });
    },
  },
};
</script>
<style lang="scss" scoped>
/* 上传 */
.li-uploader {
  flex: 1;
  flex-direction: column;
}
.li-uploader-head {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
.li-uploader-info {
  color: #b2b2b2;
}
.li-uploader-body {
  margin-top: 16rpx;
}
.li-uploader__files {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}
.li-uploader__file {
  margin: 10rpx;
  width: 210rpx;
  height: 210rpx;
  position: relative;
}
.li-uploader__img {
  display: block;
  width: 210rpx;
  height: 210rpx;
}
.li-uploader__input-box {
  position: relative;
  margin: 10rpx;
  width: 208rpx;
  height: 208rpx;
  border: 2rpx solid #d9d9d9;
  background-color: #fff;
}
.li-uploader__input-box:before,
.li-uploader__input-box:after {
  content: " ";
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  background-color: #d9d9d9;
}
.li-uploader__input-box:before {
  width: 4rpx;
  height: 79rpx;
}
.li-uploader__input-box:after {
  width: 79rpx;
  height: 4rpx;
}
.li-uploader__input-box:active {
  border-color: #999999;
}
.li-uploader__input-box:active:before,
.li-uploader__input-box:active:after {
  background-color: #999999;
}
.li-uploader__input {
  position: absolute;
  z-index: 1;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
}

.icon-close {
  position: absolute;
  right: -10px;
  top: -10px;
  z-index: 9;
}
</style>
